package com.javaweb.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/9/24 11:13
 */
@Component
@Aspect
@Slf4j
public class TimeMeasureAspect {
    // pointcut
    @Pointcut("@annotation(timeMeasure)")
    public void timeMeasureForSelects(TimeMeasure timeMeasure){}

    // advice


    @Around("timeMeasureForSelects(timeMeasure)")
    public Object timeMeasureAround(ProceedingJoinPoint jp, TimeMeasure timeMeasure) throws Throwable {
        GranularType type = timeMeasure.granular() ;
        Long start = System.currentTimeMillis() ;
        Object ret = jp.proceed() ;
        if (type == GranularType.SECOND) {
            log.info("method: " + jp.getSignature().getName() + ", used (s): "
                    + (System.currentTimeMillis() - start ) / 1000.0 ) ;
        } else {
            log.info("method: " + jp.getSignature().getName() + ", used (ms): "
                    + (System.currentTimeMillis() - start ) ) ;
        }
        return ret ;
    }

//    @Before("")
//    public void before(JoinPoint jp) {
//
//    }




}
